package com.clouddot.baipai.ui.fragment.V_MainFragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.model.beam.getRecord.attentionRecord;
import com.clouddot.baipai.model.beam.getRecord.classAttendRecord;
import com.clouddot.baipai.model.beam.getRecord.getRecordByIdResponse;
import com.clouddot.baipai.model.beam.getRecord.getRecordResponse;
import com.clouddot.baipai.ui.activity.V_MainActivity;
import com.clouddot.baipai.ui.adapter.A_recordByIdRecyclerAdapter;
import com.clouddot.baipai.ui.adapter.A_recordRecyclerAdapter;
import com.clouddot.baipai.ui.fragment.BaseFragment;
import com.clouddot.baipai.ui.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wxy on 2018/4/10.
 */

public class V_AttendableFragment extends BaseFragment {
    private View view;
    private Context context;
    private RecyclerView recyclerView;
    private A_recordByIdRecyclerAdapter classRecyclerAdapter;
    private A_recordRecyclerAdapter classRecyclerAdapter1;
    private List<attentionRecord> list  = new ArrayList<attentionRecord>();
    private List<classAttendRecord> list1  = new ArrayList<classAttendRecord>();

    private LinearLayout last,next;
    private boolean isRunning = false;
    private int pageCount;
    private int CurrentPage = 1;
//    private RelativeLayout scanCard;
    private ConstraintLayout constraintLayout3,constraintLayout1;
    private RelativeLayout relativeLayout;
    private TextView textView,title;


    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        return view;
    }

    @Override
    protected void setContentView() {
        super.setContentView();

        view = LayoutInflater.from(context).inflate(R.layout.v_f_attendancefg,null);
    }

    @Override
    protected void initControls() {
        super.initControls();
        recyclerView = view.findViewById(R.id.recycle);
//        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.setLayoutManager(new GridLayoutManager(context,3));
//        recyclerView.addItemDecoration(new DivierItemGrid(context));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        classRecyclerAdapter = new A_recordByIdRecyclerAdapter(context,list);
        recyclerView.setAdapter(classRecyclerAdapter);

        last = view.findViewById(R.id.last);
        next = view.findViewById(R.id.next);

        relativeLayout = view.findViewById(R.id.loading);
        ImageView loading = view.findViewById(R.id.imgloading);
        loading.startAnimation(loadingAnim);

//        scanCard = view.findViewById(R.id.scanCard);

        constraintLayout1 = view.findViewById(R.id.content);
        constraintLayout3 = view.findViewById(R.id.content1);
        constraintLayout1.startAnimation(animation3);
        textView = view.findViewById(R.id.classNum);
        title = view.findViewById(R.id.title);

    }

    @Override
    protected void setListener() {
        super.setListener();
        last.setOnClickListener(lastListener);
        next.setOnClickListener(nextListener);
    }

    @Override
    protected void loadParams() {
        super.loadParams();
    }

    @Override
    public void onAttach(Context context) {
        this.context = context;
        super.onAttach(context);
    }

    @Override
    public void onResume() {
        super.onResume();
        if(Env.getAppContext().isInitial) {
                constraintLayout1.setVisibility(View.VISIBLE);
                relativeLayout.setVisibility(View.VISIBLE);
                constraintLayout3.setVisibility(View.GONE);
                runThreadAction();
        }

    }

    @Override
    public void onPause() {
        super.onPause();

    }

    @Override
    public void runThreadAction() {
        super.runThreadAction();
    }

    @Override
    protected Object runThreadActionExceute() {
        super.runThreadActionExceute();
        if(Env.getAppContext().StudentId != null){
            return Env.getAppPresenter().getRecordBySid(CurrentPage,Env.getAppContext().itemCount * 2);
        }else{
            return Env.getAppPresenter().getRecord();

        }
    }

    @Override
    protected void runThreadActionSuccess( Object result) {
        super.runThreadActionSuccess(result);
        relativeLayout.setVisibility(View.GONE);
        textView.setVisibility(View.GONE);
        if(Env.getAppContext().StudentId != null) {
            title.setText("考勤记录");
            getRecordByIdResponse response = (getRecordByIdResponse) result;
            if (response.isSuccess()) {
                list = response.list;
                pageCount = Integer.parseInt(response.PageCount);
                classRecyclerAdapter = new A_recordByIdRecyclerAdapter(context, list);
                recyclerView.setAdapter(classRecyclerAdapter);

            } else {
                ToastUtil.showToast(context, response.getErrMsg());
                isRunning = true;
                return;
            }
        }else{
            title.setText("班级考勤情况");
            getRecordResponse response = (getRecordResponse) result;
            if (response.isSuccess()) {
                list1 = response.list;
                int i = 0;
                for(classAttendRecord classAttendRecord : response.list){
                    if(classAttendRecord.getCardtimes() > 0) i++;
                }
                textView.setVisibility(View.VISIBLE);
                textView.setText("全班人数"+list1.size()+",已签到"+i+"人");
                classRecyclerAdapter1 = new A_recordRecyclerAdapter(context, list1);
                recyclerView.setAdapter(classRecyclerAdapter1);
            } else {
                ToastUtil.showToast(context, response.getErrMsg());
                isRunning = true;
                return;
            }

        }
        constraintLayout3.setVisibility(View.VISIBLE);
        isRunning = true;

    }

    @Override
    protected void runThreadActionError(Exception ex) {
        super.runThreadActionError( ex);
    }

    private View.OnClickListener lastListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            V_MainActivity.backTime = Env.getAppContext().backMainTime * 60;
            if (CurrentPage <= 1){
                ToastUtil.showToast(context,"当前是第一页");
                return;
            }
            isRunning = false;
            relativeLayout.setVisibility(View.VISIBLE);
            constraintLayout3.setVisibility(View.GONE);
            CurrentPage--;
            runThreadAction();
        }
    };

    private View.OnClickListener nextListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            V_MainActivity.backTime = Env.getAppContext().backMainTime * 60;
            if (CurrentPage >= pageCount){
                ToastUtil.showToast(context,"当前是最后一页");
                return;
            }
            isRunning = false;
            relativeLayout.setVisibility(View.VISIBLE);
            constraintLayout3.setVisibility(View.GONE);
            CurrentPage++;
            runThreadAction();
        }
    };
}
